package com.xbongbong.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.errorcodes.PurchaseErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;

import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 * @author 吴峰
 * @date 2021/07/19 15:35
 */
public class PurchaseUtil {
    private final static double DOUBLE_ZERO = 0D;

    private final static int ZERO = 0;
    /**
     * beforeSave中的计算成本
     * @param validateDataDTO
     * @throws XbbException
     */
    public static void calculatePurchaseCost(ValidateDataDTO validateDataDTO, boolean isNew) throws XbbException {
        Long purchaseId = validateDataDTO.getDataId();
        JSONObject purchaseData = validateDataDTO.getData();
        //需要计算一下未付款金额/未开票金额/付款比例
        if ( !isNew ) {
            Double amount = getDoubleOrDefaultFromFormData(purchaseData, PurchaseEnum.TOTAL_MONEY.getAttr(), 0D);
            Double finishAmount = getDoubleOrDefaultFromFormData(purchaseData, PurchaseEnum.FINISH_AMOUNT.getAttr(), 0D);
            Double badAmount = getDoubleOrDefaultFromFormData(purchaseData, PurchaseEnum.BAD_AMOUNT.getAttr(), 0D);
            Double unAmount = amount - finishAmount - badAmount;
            Double invoiceMoney = getDoubleOrDefaultFromFormData(purchaseData, PurchaseEnum.INVOICE_AMOUNT.getAttr(), 0D);
            double payPercent;
            //合同金额为0时候 收款比例为100%
            if ( amount == 0 ) {
                payPercent = 1;
            } else {
                payPercent = Arith.round(Arith.div(finishAmount, amount), 4);
            }
            purchaseData.put(PurchaseEnum.UN_FINISH_AMOUNT.getAttr(), unAmount);
            purchaseData.put(PurchaseEnum.RECEINE_PERCENT.getAttr(), payPercent);
            purchaseData.put(PurchaseEnum.UN_INVOICE_AMOUNT.getAttr(), amount - invoiceMoney);
        }
    }


    /**
     * 获取采购合同审批时付款计划、付款单、预付款的id
     *
     * @param jsonArray
     * @return
     */
    public static void getFlowStatusIdList(ValidateDataDTO validateDataDTO, JSONArray jsonArray, List<Long> flowStatusIdList) {
        Long taskId = validateDataDTO.getTaskId();
        if (Objects.isNull(taskId) || taskId.equals(BasicConstant.ZERO_LONG)) {
            //代表是审批中
            for (int i = BasicConstant.ZERO; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (Objects.nonNull(jsonObject)) {
                    Object idObj = jsonObject.get(BasicConstant.ID);
                    if (Objects.nonNull(idObj)) {
                        flowStatusIdList.add(Long.parseLong(String.valueOf(idObj)));
                    }
                }
            }
        }
    }

    /**
     * 采购合同参数保存前对相关参数进行合理化校验
     *
     * @param validateDataDTO 前端传参数据
     * @throws XbbException 参数不符合要求错误提醒
     * @author Jian.Wang
     * @date 2019/1/30 15:32
     * @since v1.0
     *
     * */
    public static void parameterCheck(ValidateDataDTO validateDataDTO) throws XbbException {
        JSONObject data = validateDataDTO.getData();
        if (Objects.isNull(data)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        Double totalMoney = StringUtil.toDouble(FastJsonHelper.getStringFromFormData(data, PurchaseEnum.TOTAL_MONEY.getAttr()), 0D);
        if (Double.compare(totalMoney, DOUBLE_ZERO) < ZERO) {
            throw new XbbException(PurchaseErrorCodeEnum.API_ERROR_225029);
        }
    }
}
